/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import entities.User;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author viev
 */
public class ChoseUserTableModel extends AbstractTableModel {

    ArrayList<User> list;
    ArrayList<User> disp;
    private int mode;
    public static int SELECT_ALL_MODE = 1;
    public static int SELECT_HEAD_MODE = 2;
    public static int SELECT_HEAD_AND_EMPLOYEE_MODE = 3;
    public static int SELECT_ALL_EMPLOYEE_MODE = 4;
    public static int SELECT_EMPLOYEE_OF_DEPARTMENT_MODE = 5;

    public ChoseUserTableModel(int mode) {
        list = new ArrayList<User>();
        disp = new ArrayList<User>();
    }

    public ChoseUserTableModel(ArrayList<User> list, int mode) {
        this.list = new ArrayList<User>(list);
        this.disp = new ArrayList<User>(list);
        this.mode = mode;
    }

    @Override
    public int getRowCount() {
        return disp.size();
    }

    @Override
    public int getColumnCount() {
        switch (mode) {
            case 1:
                return 4;
            case 2:
                return 4;
            case 3:
                return 0;
            case 4:
                return 0;
            case 5:
                return 4;
            default:
                return 0;
        }
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (mode) {
            case 1:
                return switchMode1(rowIndex, columnIndex);
            case 2:
                return switchMode2(rowIndex, columnIndex);
            case 3:
                return switchMode3(rowIndex, columnIndex);
            case 4:
                return switchMode4(rowIndex, columnIndex);
            case 5:
                return switchMode5(rowIndex, columnIndex);
            default:
                return null;
        }
    }

    private Object switchMode1(int rowIndex, int columnIndex) {
        User user = disp.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return user.getLoginID();
            case 1:
                return user.getName();
            case 2:
                return user.getPhone();
            case 3:
                return user.getEmailAddress();
            default:
                return null;
        }
    }

    private Object switchMode2(int rowIndex, int columnIndex) {
        String levels[] = {"Head", "Immediate Head", "Next Immediate Head", "Junior Head "};
        User user = disp.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return user.getLoginID();
            case 1:
                return user.getName();
            case 2:
                return user.getDepartmentName();
            case 3:
                return levels[user.getLevel() - 1];
            default:
                return null;
        }
    }

    private Object switchMode3(int rowIndex, int columnIndex) {
        return null;
    }

    private Object switchMode4(int rowIndex, int columnIndex) {
        return null;
    }

    private Object switchMode5(int rowIndex, int columnIndex) {
        User user = disp.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return user.getLoginID();
            case 1:
                return user.getName();
            case 2:
                return user.getPhone();
            case 3:
                return user.getEmailAddress();
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public String getColumnName(int column) {
        switch (mode) {
            case 1:
                return getColumnNameMode1(column);
            case 2:
                return getColumnNameMode2(column);
            case 3:
                return getColumnNameMode3(column);
            case 4:
                return getColumnNameMode4(column);
            case 5:
                return getColumnNameMode5(column);
            default:
                return null;
        }
    }

    private String getColumnNameMode1(int column) {
        switch (column) {
            case 0:
                return "ID";
            case 1:
                return "Name";
            case 2:
                return "Phone";
            case 3:
                return "Email Address";
            default:
                return null;
        }
    }

    private String getColumnNameMode2(int column) {
        switch (column) {
            case 0:
                return "ID";
            case 1:
                return "Name";
            case 2:
                return "Department";                
            case 3:
                return "Level";
            default:
                return null;
        }
    }

    private String getColumnNameMode3(int column) {
        return null;
    }

    private String getColumnNameMode4(int column) {
        return null;
    }

    private String getColumnNameMode5(int column) {
        switch (column) {
            case 0:
                return "ID";
            case 1:
                return "Name";
            case 2:
                return "Phone";
            case 3:
                return "Email";
            default:
                return null;
        }
    }

    public void addElement(User user) {
        list.add(user);
        fireTableDataChanged();
    }

    public User getAt(int index) {
        return disp.get(index);
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }

    public void filter(String id, String name, int depID, int role, int level) {
        disp.clear();
        for (User u : list) {
            boolean accept = true;
            if (id != null && id.length() != 0) {
                int length = id.length();
                if (u.getLoginID().length() >= length) {
                    if (!u.getLoginID().substring(0, length).equalsIgnoreCase(id)) {
                        accept = false;
                    }
                } else {
                    accept = false;
                }
            }
            if (name != null && name.length() != 0) {
                int length = name.length();
                if (u.getName().length() >= length) {
                    if (!u.getName().substring(0, length).equalsIgnoreCase(name)) {
                        accept = false;
                    }
                } else {
                    accept = false;
                }
            }
            if (depID != 0) {
                if (u.getDepartmentID() != depID) {
                    accept = false;
                }
            }
            if (role != 0) {
                if (u.getRole() != role + 1) {
                    accept = false;
                }
            }
            if (level != 0) {
                if (u.getLevel() != level) {
                    accept = false;
                }
            }
            if (accept) {
                disp.add(u);
            }
        }
        fireTableDataChanged();
    }
}
